package xyz.gzdxc.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {
    private static final Logger LOG = LoggerFactory.getLogger(DateUtil.class);

    /**
     * get the time difference in min
     *
     * @param endDate
     * @return
     */
    public static long getTimeDiffInMin(Date endDate) {
        Date date = new Date();
        //the milliseconds of a minute
        long msInAMin = 1000 * 60;
        LOG.info("now timestamp => {}", date.getTime());
        LOG.info("endDate timestamp => {}", endDate.getTime());
        //get the difference of the two time
        long diff = date.getTime() - endDate.getTime();
        long min = 0;
        //calculate the diff min
        min = diff / msInAMin;
        LOG.info("now endDate diff in min => {}", min);
        return min;
    }

    /**
     * calculate the session timeout date
     *
     * @param loginDate
     * @param sessionTimeoutHour
     * @return the session timeout date
     */
    public static Date calculateSessionTimeoutDate(Date loginDate, int sessionTimeoutHour){
        Calendar calendar = new GregorianCalendar();
        LOG.info("the login date => {}", loginDate);
        LOG.info("the session timeout hour => {}", sessionTimeoutHour);
        calendar.setTime(loginDate);
        calendar.add(Calendar.HOUR, sessionTimeoutHour);
        Date sessionTimeoutDate = calendar.getTime();
        LOG.info("the session timeout date => {}", sessionTimeoutDate);
        return sessionTimeoutDate;
    }
}
